package com.traffic.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.traffic.commons.base.BaseController;
import com.traffic.commons.utils.DateUtils;
import com.traffic.commons.utils.FtpUtil;
import com.traffic.commons.utils.PageInfo;
import com.traffic.model.TrafficInformation;
import com.traffic.service.ITrafficInformationService;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * <p>
 * 信息公告表  前端控制器
 * </p>
 *
 * @author Hongyue Wang
 * @since 2017-04-20
 */
@Controller
@RequestMapping("/trafficInformation")
public class TrafficInformationController extends BaseController {

    @Autowired
    private ITrafficInformationService trafficInformationService;

    @GetMapping("/manager")
    public String manager() {
        return "admin/trafficInformation/trafficInformationList";
    }

    private static final String propertiesPath = "properties/ftp.properties";

    private static final String imgPath = File.separator + "information";

    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(TrafficInformation trafficInformation, Integer page, Integer rows, String sort, String order) {
        trafficInformation.setIsDelete("N");
        EntityWrapper<TrafficInformation> ew = new EntityWrapper<TrafficInformation>();
        ew.setEntity(trafficInformation);
        Page<TrafficInformation> pages = getPage(page, rows, sort, order);
        try {
            pages = trafficInformationService.selectPage(pages, ew);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageToPageInfo(pages);
    }

    /**
     * 添加页面
     *
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/trafficInformation/trafficInformationAdd";
    }

    /**
     * 添加
     *
     * @param
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(TrafficInformation trafficInformation, @RequestParam(value = "file") CommonsMultipartFile[] files) {
        trafficInformation.setCreatTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficInformation.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficInformation.setContent(StringEscapeUtils.unescapeHtml(trafficInformation.getContent()));
        trafficInformation.setIsDelete("N");
        String imgName = "";
        boolean b = false;
        try {
            for(int i = 0;i < files.length ; i++){
                DiskFileItem fileItem = (DiskFileItem)files[i].getFileItem();
                File file = fileItem.getStoreLocation();
                FileInputStream in = new FileInputStream(file);
                imgName = FtpUtil.getImgName();
                boolean flag = FtpUtil.uploadFile(FtpUtil.getPropertiesValue(propertiesPath, "ftp.host"),
                        Integer.valueOf(FtpUtil.getPropertiesValue(propertiesPath, "ftp.port")),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.username"),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.password"),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.basepath"),
                        imgPath, imgName, in);
                trafficInformation.setPicPath(imgPath + File.separator  + imgName);
                b = trafficInformationService.insert(trafficInformation);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        TrafficInformation trafficInformation = new TrafficInformation();
        trafficInformation.setId(id.intValue());
        trafficInformation.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficInformation.setIsDelete("Y");
        boolean b = trafficInformationService.updateById(trafficInformation);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }

    /**
     * 编辑
     *
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        TrafficInformation trafficInformation = trafficInformationService.selectById(id);
        model.addAttribute("trafficInformation" , trafficInformation);
        return "admin/trafficInformation/trafficInformationEdit";
    }

    /**
     * 编辑
     *
     * @param
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(TrafficInformation trafficInformation, @RequestParam(value = "file") CommonsMultipartFile[] files) {
        trafficInformation.setEditTime(DateUtils.genMkDate("yyyy-MM-dd HH:mm:ss"));
        trafficInformation.setContent(StringEscapeUtils.unescapeHtml(trafficInformation.getContent()));
        String imgName = "";
        boolean b = false;
        try {
            for(int i = 0;i < files.length ; i++){
                DiskFileItem fileItem = (DiskFileItem)files[i].getFileItem();
                File file = fileItem.getStoreLocation();
                FileInputStream in = new FileInputStream(file);
                imgName = FtpUtil.getImgName();
                boolean flag = FtpUtil.uploadFile(FtpUtil.getPropertiesValue(propertiesPath, "ftp.host"),
                        Integer.valueOf(FtpUtil.getPropertiesValue(propertiesPath, "ftp.port")),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.username"),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.password"),
                        FtpUtil.getPropertiesValue(propertiesPath, "ftp.basepath"),
                        imgPath, imgName, in);
                trafficInformation.setPicPath(imgPath + File.separator  + imgName);
                b = trafficInformationService.updateById(trafficInformation);            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

}
